from PySide6.QtWidgets import QWidget, QMenu
from PySide6.QtCore import Qt, Property, QRectF, QSize, QPropertyAnimation, Signal
from PySide6.QtGui import QColor, QPainter, QPen, QPixmap


class Loading(QWidget):

    stopRequested = Signal()

    def __init__(self, image_path):
        super().__init__()
        self._value = 0
        self.image = QPixmap(image_path)  # 加载图片
        self.progress_size = QSize(
            self.image.height(), self.image.height()
        )  # 进度条尺寸
        # 设置固定尺寸（图片宽度 + 进度条区域宽度）
        self.setFixedSize(
            self.image.width() + self.progress_size.height() + 30,
            self.image.height() + 30,
        )
        self.progress_width = self.height() // 10  # 进度条宽度
        self.progress_color = QColor(255, 255, 255)
        self.text_color = QColor(255, 255, 255)
        self.max_value = 100

        # 右键菜单
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.show_context_menu)

    def show_context_menu(self, pos):
        menu = QMenu(self)

        # 设置菜单样式表
        menu.setStyleSheet(
            """
            QMenu {
                color:black;
                background-color:white; 
                border-radius:3px;
                padding:5px;
                margin:6px;
            }
            QMenu::item:text { 
                padding-left:10px;
                padding-right:10px;
            }
            QMenu::item:selected{ 
                color:#1aa3ff;
                background-color: #e5f5ff;
                border-radius:3px;
            }
            QMenu::separator{
                height:1px;
                background:#bbbbbb;
                margin:5px;
                margin-left:10px;
                margin-right:10px;
            }
        """
        )
        menu.setWindowFlag(Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
        menu.setAttribute(Qt.WA_TranslucentBackground)
        stop_action = menu.addAction("中止")
        stop_action.triggered.connect(self.stopRequested.emit)
        menu.exec_(self.mapToGlobal(pos))

    def get_value(self):
        return self._value

    def set_value(self, value):
        self._value = value
        self.repaint()

    value = Property(int, get_value, set_value)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # 绘制图片（左侧）
        painter.drawPixmap(0, 0, self.image)

        # 计算进度条位置（右侧）
        progress_x = self.image.width()
        progress_y = 0
        # 绘制进度条
        pen = QPen()
        pen.setWidth(self.progress_width)
        pen.setColor(self.progress_color)
        painter.setPen(pen)
        start_angle = 90 * 16
        span_angle = -self._value / self.max_value * 360 * 16
        painter.drawArc(
            progress_x + self.progress_width,
            progress_y + self.progress_width,
            self.progress_size.width() - 2 * self.progress_width,
            self.progress_size.height() - 2 * self.progress_width,
            start_angle,
            span_angle,
        )

        # 绘制百分比文字
        painter.setPen(self.text_color)
        painter.setFont(self.font())
        text_rect = QRectF(
            progress_x + self.progress_width,
            progress_y + self.progress_width,
            self.progress_size.width() - 2 * self.progress_width,
            self.progress_size.height() - 2 * self.progress_width,
        )
        painter.drawText(text_rect, Qt.AlignCenter, f"{self._value}%")
